import {
	addToShppingCar
} from "./api/goods.js";
import {
	joinAct
} from "./api/activity.js";
import {
	ActType
} from "./common-data.js";
import authCache from "@/uni-sdk/utils/authCache.js";
import { getCommunityList } from "./api/login.js";
import { createOrder, getPayInfo, getOrderDetail } from '@/base/api/member.js';

/**
 * 加入购物车
 */
export async function handleToShoppingCar(productItem, options) {
	options = Object.assign({
		quantity: 1
	}, options || {});
	const community = authCache.getCommunity();
	const re = await addToShppingCar({
		communityId: community ? community.id : null,
		productId: productItem.id,
		quantity: options.quantity
	});
	if (re) {
		getApp().globalData.toast('添加成功', true);
		getApp().globalData.setUpdatePage("pages/home/shopping-bag/shopping-bag");
	}
	return re;
}
/**
 * @param {Object} data.orderData 商品数据或优惠券数据
 * @param {Object} data.shoppingCartIds 购物车数据id数组
 * @param {Object} data.actData 活动数据
 */
export function toSubmitOrder(data, isCoupon) {
	uni.navigateTo({
		url: isCoupon ? "/pages/submit-coupon-order/submit-coupon-order" : "/pages/submit-order/submit-order",
		success(e) {
			e.eventChannel.emit('data', data);
		},
	})
}

export function handleImgRichText(str) {
	str = str.replace(/width=[\"\'][\d]+[\"\']/g, "width='100%'");
	str = str.replace(/height=[\"\'][\d]+[\"\']/g, "height='auto'");
	return str;
}

/**
 * @param {Object} actionText
 * @param {Object} data
 * @param {Object} callback
 */
export function handleJoinAct(actionText, data, callback) {
	// if (data.activityType !== ActType.jifen && data.activityType !== ActType.chang) {
	// 	getApp().globalData.toast("开发中...");
	// 	return null;
	// }
	const community = authCache.getCommunity();
	if (!community) return;
	getApp().globalData.confirm(`确认${actionText}吗？`, {
		async ok(){
			if(data.activityType === ActType.tuan || data.activityType === ActType.qiang) {
				toSubmitOrder({
					orderData: [data.goods],
					shoppingCartIds: [],
					actData: data,
				}, data.activityType === ActType.qiang);
			} else {
				
			}
			// const re = await joinAct(data.id, community.id);
			// callback && callback(re);
		}
	});
}

let communityData = null;
export async function handleCommunityData(isReset){
	if(isReset) {
		communityData = null;
		return;
	}
	if(communityData) return communityData;
	const list = await getCommunityList();
	if(list) {
		communityData = list;
	}
	return communityData || [];
}

export function getOrderStatusText(status){
	const arr = ["待支付","待配送", "已收货", "已取消", "退款中", "已退款", "已超时", "支付失败"];
	return arr[status];
}

export async function handlePay(orderNo, callback){
	const payInfo = await getPayInfo(orderNo);
	if (payInfo) {
		uni.requestPayment({
			provider: 'wxpay',
			appid: payInfo.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
			timeStamp: payInfo.timeStamp,
			nonceStr: payInfo.nonceStr, // 随机字符串
			package: payInfo.packageContent, // 固定值
			signType: payInfo.signType,
			paySign: payInfo.paySign,
			success(res) {
				callback && callback(true);
			},
			fail(e) {
				console.log("===============>handlePay fail", e, payInfo);
				callback && callback(false);
			},
		});
	}
}